#include <stdio.h>
#include "querysolar_2.h"

// 这里先简化GetGre接口和实现，返回公历年月日（调用者传入指针接收）
int GetGre(int lunarYear, int lunarMonth, int lunarDay, int* solarYear, int* solarMonth, int* solarDay);

// 农历天干地支计算辅助（简化版）
const char* HeavenlyStems[] = {"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};
const char* EarthlyBranches[] = {"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"};

void printGanZhi(int lunarYear, int lunarMonth, int lunarDay) {
    int ganYear = (lunarYear - 4) % 10;
    int zhiYear = (lunarYear - 4) % 12;
    int ganMonth = (lunarYear * 12 + lunarMonth + 3) % 10;  // 计算简化处理
    int zhiMonth = (lunarMonth + 1) % 12;
    int ganDay = (lunarYear * 365 + lunarMonth * 30 + lunarDay) % 10; // 粗略估计
    int zhiDay = (lunarYear * 365 + lunarMonth * 30 + lunarDay) % 12;

    printf("农历天干地支表示:\n");
    printf("  年干支: %s%s年\n", HeavenlyStems[ganYear], EarthlyBranches[zhiYear]);
    printf("  月干支: %s%s月\n", HeavenlyStems[ganMonth], EarthlyBranches[zhiMonth]);
    printf("  日干支: %s%s日\n", HeavenlyStems[ganDay], EarthlyBranches[zhiDay]);
}

// 校验农历日期有效性（这里只简单判断年份范围及月份日期范围）
int checkLunarDate(int y, int m, int d) {
    if (y < 1840 || y > 2100) {
        printf("错误：年份应在1840~2100之间。\n");
        return 0;
    }
    if (m < 1 || m > 12) {
        printf("错误：月份应在1~12之间。\n");
        return 0;
    }
    if (d < 1 || d > 30) {
        printf("错误：日期应在1~30之间。\n");
        return 0;
    }
    return 1;
}

// 查询函数主入口
void queryGregorianFromLunar(void) {
    int y,m,d;
    printf("请输入农历日期（格式：年 月 日，空格分隔，年份范围1840~2100）: ");
    if(scanf("%d %d %d", &y, &m, &d) != 3) {
        printf("输入格式错误！\n");
        return;
    }

    if (!checkLunarDate(y,m,d)) return;

    printGanZhi(y,m,d);

    int sy, sm, sd;
    if (GetGre(y, m, d, &sy, &sm, &sd)) {
        printf("对应的公历日期是：%d-%02d-%02d\n", sy, sm, sd);
    } else {
        printf("无法转换该农历日期。\n");
    }
}

// ----------- 模拟 GetGre 函数实现示例 ------------
// 真实环境请替换为基于农历数据表的精准转换函数
int GetGre(int lunarYear, int lunarMonth, int lunarDay, int* solarYear, int* solarMonth, int* solarDay) {
    // 这里模拟简单规则，示例为农历日期+offset天，实际上需要用完整农历数据表转换
    // 真实代码你可调用之前我帮你写的农历转公历算法
    
    // 这个模拟不准确，仅做示例：
    *solarYear = lunarYear;
    *solarMonth = lunarMonth;
    *solarDay = lunarDay + 10;
    if (*solarDay > 30) {
        *solarDay -= 30;
        (*solarMonth)++;
    }
    if (*solarMonth > 12) {
        *solarMonth -= 12;
        (*solarYear)++;
    }
    return 1; // 成功
}
